const express = require("express")
const multer = require("multer")
const { UploadError } = require("../utils/error")
const { upload, formatResponse } = require("../utils/tool")

const router = express.Router()

router.post("/", async function(req, res, next) {
    // 上传控件的name值
    upload.single("file")(req, res, function(err) {
        if (err instanceof multer.MulterError) {
            next(new UploadError("上传文件失败"))
        } else {
            const path = "/static/uploads/" + req.file.filename
            res.send(formatResponse(0, "", path))
        }
    })
})


module.exports = router